﻿
IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Mail_ClearFolder_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Mail_ClearFolder_V2];
GO

CREATE PROCEDURE [dbo].[sproc_Mail_ClearFolder_V2]
(
    @UserName   nvarchar(50),
    @FolderType int
)
AS
BEGIN

SET NOCOUNT ON;
SET XACT_ABORT ON;

-- Get a list of mails to delete
/*
 * XML format
 * <mails>
 *  <m id="" />
 * </mails>
 */
DECLARE @xml xml;
SELECT TOP 1 @xml = R.x
    FROM
    (
        SELECT MailId as N'@id'
            FROM dbo.UDS_Mail
            WHERE UserName = @UserName AND FolderType = @FolderType
            FOR XML PATH(N'm'), ROOT(N'mails')
    ) as R(x);

exec dbo.sproc_Mail_Delete_V2
    @mailIdXml = @xml,
    @physicalRemove = 1;  -- physically remove all the mails

END
GO
